package com.basic.tools.codegen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.tools.codegen.entity.*;
import com.basic.tools.codegen.entity.domain.GenUtils;
import com.basic.tools.codegen.mapper.GenTableMapper;
import com.basic.tools.codegen.mapper.SysDictTypeMapper;
import com.basic.tools.codegen.mapper.TemplateGroupMapper;
import com.basic.tools.codegen.service.DatasourceConfigService;
import com.basic.tools.codegen.service.TemplateConfigService;
import com.basic.tools.template.VelocityUtil;
import com.basic.tools.template.entity.TemplateBridge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

@Controller
@RequestMapping("/admin")
public class AdminControlerWeb {

    @Autowired
    private DatasourceConfigService datasourceConfigService;

    @Autowired
    GenTableMapper genTableMapper;

    @Autowired
    TemplateConfigService templateConfigService;

    @Autowired
    SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    TemplateGroupMapper templateGroupMapper;


    @GetMapping("/templateGroup")
    public String goTemplateGroup(Model model) {
        return "templateGroup";
    }

    @GetMapping("/template/{id}")
    public String goTemplate(@PathVariable("id") Long id, Model model) {
        model.addAttribute("groupId", id);
        List<TemplateGroup> templateGroups = templateGroupMapper.listAll();
        model.addAttribute("templateGroups", templateGroups);
        return "template";
    }

    @GetMapping("/templateDetail/{id}")
    public String goTemplateDetail(@PathVariable("id") Integer id, Model model) {
        TemplateConfig tc = templateConfigService.getById(id);
        model.addAttribute("tc", tc);
        return "templateDetail";
    }


    @GetMapping("/index")
    public String goAdmin(Model model) {
        List<DatasourceConfig> datasourceConfigs =
                datasourceConfigService.listAll();

        model.addAttribute("datasourceConfigs", datasourceConfigs);
        return "index";
    }

    @GetMapping("/listTable")
    public String golist(Model model) {
        List<DatasourceConfig> datasourceConfigs =
                datasourceConfigService.listAll();
        List<TemplateGroup> templateGroups = templateGroupMapper.listAll();
        model.addAttribute("templateGroups", templateGroups);
        model.addAttribute("datasourceConfigs", datasourceConfigs);
        return "listTable";
    }

    @GetMapping("/genTable/{id}")
    public String goTableColumn(@PathVariable("id") Long id, Model model) {
        GenTable genTable = genTableMapper.selectGenTableById(id);
        QueryWrapper<SysDictType> qw=new QueryWrapper();
        List<SysDictType> list = sysDictTypeMapper.selectList(qw);
        model.addAttribute("genTable", genTable);
        model.addAttribute("dicts", list);
        return "genTable";
    }

    @GetMapping("/previewCode/{id}")
    public String previewCode(@PathVariable("id") Long tableId, Model model) {
        GenTable genTable = genTableMapper.selectGenTableById(tableId);
        List<TemplateBridge> listt = GenUtils.getTemplateBridges(genTable);
        model.addAttribute("genMap", VelocityUtil.generatorCodeMap(listt));
        return "previewCode";
    }

    @GetMapping("/previewCodeByOnline/{id}/{gid}")
    public String previewCodeByOnline(@PathVariable("id") Long id, @PathVariable("gid") Long gid, Model model) throws IOException {
        //gid为模板组id
        GenTable genTable = genTableMapper.selectGenTableById(id);
        gid = Long.parseLong(genTable.getGid());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        List<TemplateConfig> templateConfigs = templateConfigService.listByGroupId(gid + "");
        Map<String, String> remap =GenUtils.generTableByOnline(templateConfigs, genTable, zip,templateConfigService);
        model.addAttribute("genMap", remap);
        return "previewCodeByOnline";
    }


    //新增数据源
    @GetMapping("/addSource")
    public String goAddSource(Model model) {
        return "addSource";
    }

    @GetMapping("/editSource/{id}")
    public String godataSource(@PathVariable Integer id, Model model) {
        DatasourceConfig datasourceConfig = datasourceConfigService.getById(id);
        model.addAttribute("datasourceConfig", datasourceConfig);
        return "editSource";
    }


    //字典类型
    @GetMapping("/dict")
    public String goDict(Model model) {
        return "dict";
    }

    //字典数据
    @GetMapping("/dictData")
    public String goDictData(Model model) {
        return "dictData";
    }

    //字典数据
    @GetMapping("/formDesigner")
    public String goFormDesigner(Model model) {
        return "formDesigner/index";
    }


}
